package com.diversitopia.base;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public abstract class BaseDiversitopiaDao {
	protected Map<String, String> xpaths;
	protected Object buildObject(Document file, String className) {
		
		return new Object();
	}
	protected Document parseFile(String filename) throws Exception {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory
				.newInstance();
		domFactory.setNamespaceAware(true);
		DocumentBuilder builder;
		try {
			builder = domFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			//
			throw new Exception(
					"ParserConfigurationException creating builder: ", e);
		}

		File f = new File(filename);

		if (!f.exists()) {
			//
		}

		try {
			if (f.exists()) {

				return builder.parse(f.toURI().toString());
				// return XmlParser.getInstance().parseFile(f);} 
			} else {
				throw new Exception("File '" + filename
						+ "' doesn't exist and loadFromClasspath not enabled");
			}
		} catch (SAXException e) {
			//log.error("SAXException parsing xml for '" + filename + "', uri: '"	+ f.toURI().toString() + "'" + e);
			throw new Exception("SAXException evaluating xpaths: ", e);
		} catch (IOException e) {
			//log.error("IOException parsing xml: for '" + filename + "', uri: '"	+ f.toURI().toString() + "'" + e);
			throw new Exception("IOException evaluating xpaths: ", e);
		} catch (URISyntaxException e) {
			//log.error("URI Syntax Exception attempting to retrieve file '"			+ filename + "' from classpath");
			throw new Exception(
					"URI Syntax exception retrieving file classpath: ", e);

		}
	}
	public Map<String, String> getXpaths() {
		return xpaths;
	}

	public void setXpaths(Map<String, String> xpaths) {
		this.xpaths = xpaths;
	}
}
